<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="HAddfn">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { assignRoles } from '@/api/employess'
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    currentid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.getRoles()
    this.getUserDetailById()
  },
  methods: {
    // 给父组件调用的方法
    getgetUserDetailById() {
      this.getUserDetailById()
    },
    //  点击添加把当前选中传给后台
    async  HAddfn() {
      const re = await assignRoles({ id: this.currentid,
        roleIds: this.roleIds })
      console.log(re)
      this.$emit('close') // 通知后台关闭弹窗
    },
    // 获取当前点击的角色
    async  getUserDetailById() {
    //   console.log(this.currentid)
      const re = await getUserDetailById(this.currentid)
      this.roleIds = re.data.roleIds // 通过接口把当前有的角色显示出来
      console.log(re)
    },
    // 获取当前所有角色
    async getRoles() {
      const re = await getRoles({ page: 1, pagesize: 1000 })
      this.list = re.data.rows
      console.log(re)
    },
    // 点击取消
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
<style >
.el-dialog__header {
    padding: 36px 20px 10px;
}
</style>
